package com.chen.springboot.controller.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.springboot.common.AuthAccess;
import com.chen.springboot.common.Result;
import com.chen.springboot.dto.DeleteRequest;
import com.chen.springboot.dto.PageRequestShop;
import com.chen.springboot.entity.Item;
import com.chen.springboot.service.ItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Author Chen
 * @Date 2024/12/18 20:42
 */
@Api(tags = {"商品接口"})
@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @ApiOperation("查询所有的接口")
    @GetMapping("/getAll")
    public Result<?> getAll(@RequestParam(defaultValue = "") String search) {
        List<Item> items = itemService.getAll(search);
        return Result.success(items);
    }

    @ApiOperation("新增商品")
    @PostMapping("/save")
    public Result<?> saveItem(@RequestBody Item item) {
        itemService.saveItem(item);
        return Result.success();
    }

    @ApiOperation("更新商品信息")
    @PutMapping("/update")
    public Result<?> update(@RequestBody Item item) {
        itemService.update(item);
        return Result.success();
    }
//单个删除
    @ApiOperation("删除商品信息")
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam Long id) {
        itemService.delete(id);
        return Result.success();
    }
    //批量删除
    @ApiOperation("删除批量商品信息")
    @PostMapping ("/deletes")
    public Result<?> deletes(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() == null || deleteRequest.getId().isEmpty()) {
            return null;
        }
        itemService.deleteBatch(deleteRequest.getId());
        return Result.success();
    }

    @ApiOperation("获取商品")
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        Page<Item> itemPage = itemService.findPage(pageNum, pageSize, search);
        return Result.success(itemPage);
    }

    @ApiOperation("获取商品分页列表")
    @PostMapping("/page")
    public Result<?> findPages(@RequestBody PageRequestShop request) {
        Page<Item> itemPage = itemService.findPages(request.getPageNum(), request.getPageSize(), request.getTitle());
        return Result.success(itemPage);
    }

    @ApiOperation("根据id查找商品")
    @AuthAccess
    @GetMapping("/one")
    public Result<?> findById(@RequestParam Integer id) {
        Item item = itemService.findById(id);
        return Result.success(item);
    }

    @ApiOperation("根据 cid 来查询不同分类的商品")
    @AuthAccess
    @GetMapping("/category")
    public Result<?> findByCid(@RequestParam Integer cid) {
         List<Item> items=itemService.findByCid(cid);
        return Result.success(items);
    }

    //下载商品导入模板
    @ApiOperation("下载商品导入模板")
    @GetMapping("/template")
    public void downloadItemTemplate(HttpServletResponse response) {
        itemService.downloadItemTemplate(response);
    }

    //导入商品
    @ApiOperation("导入商品数据")
    @PostMapping("/import")
    public Result<?> importItem(@RequestParam("file") MultipartFile file,
                                 @RequestParam(value = "isCover", required = false) Boolean isCover) {
        try {
            itemService.importItemExcel(file, isCover != null && isCover);
            return Result.success("导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("导入失败：" + e.getMessage());
        }
    }

    //导出商品数据
    @ApiOperation("导出商品数据")
    @GetMapping("/export")
    public void exportUserData(HttpServletResponse response) {
        itemService.exportItemData(response);
    }
}